package cn.edu.zafu.bigdata.x.huangxihao202211080211;

// ShapeDemo.java
public class ShapeDemo11 {
    // 抽象类 Shape
    public abstract static class Shape {
        public abstract double area();      // 计算面积的抽象方法
        public abstract double perimeter(); // 计算周长的抽象方法

        // final 方法，不能被子类重写
        public final void display() {
            System.out.println("Shape Type: " + this.getClass().getSimpleName());
            System.out.println("Area: " + area());
            System.out.println("Perimeter: " + perimeter());
        }
    }

    // 具体形状类 Circle
    public static class Circle extends Shape {
        private double radius;

        public Circle(double radius) {
            this.radius = radius;
        }

        @Override
        public double area() {
            return Math.PI * radius * radius;
        }

        @Override
        public double perimeter() {
            return 2 * Math.PI * radius;
        }
    }

    // 具体形状类 Rectangle
    public static class Rectangle extends Shape {
        private double length;
        private double width;

        public Rectangle(double length, double width) {
            this.length = length;
            this.width = width;
        }

        @Override
        public double area() {
            return length * width;
        }

        @Override
        public double perimeter() {
            return 2 * (length + width);
        }
    }

    // final 类示例
    public final static class FinalClass {
        // final 类不能被继承
    }

    // 测试类
    public static class TestShape {
        public static void main(String[] args) {
            Shape circle = new Circle(5);
            Shape rectangle = new Rectangle(4, 6);

            circle.display();     // 调用 Circle 的 display 方法
            rectangle.display();  // 调用 Rectangle 的 display 方法

            // 测试 final 关键字
            final int finalVariable = 10;
            System.out.println("Final variable: " + finalVariable);

            // Uncommenting the following line will cause a compilation error
            // finalVariable = 20; // 这将导致编译错误
        }
    }

    // 主方法
    public static void main(String[] args) {
        TestShape.main(args);
    }
}
